package com.github.yoojia.mqtt.boot.utils;

/**
 * Write only JSON Object
 *
 * @author Yoojia Chen (yoojiachen@gmail.com)
 * @since 1.7
 */
public class SimpleJSON {

    private final StringBuilder mBuilder = new StringBuilder("{");

    public SimpleJSON(String name, Object value) {
        put(name, value);
    }

    public SimpleJSON() {
    }

    public SimpleJSON put(String name, Object value) {
        if (mBuilder.length() > 1) {
            mBuilder.append(",");
        }
        mBuilder.append("\"").append(name).append("\"");
        mBuilder.append(":");
        if (value instanceof CharSequence) {
            mBuilder.append("\"").append(value).append("\"");
        } else {
            mBuilder.append(value);
        }
        return this;
    }

    @Override
    public String toString() {
        return mBuilder.append("}").toString();
    }

    public byte[] getBytes() {
        return toString().getBytes();
    }

}
